Index


RISC World

Easy C++ Manual

Copyright © ProAction and APDL, 2001

Introduction

Easy C++ is designed for programmers who wish to learn or develop their skills in either, or both, the C or C++ programming languages. It has a very easy-to-use WIMP front-end to drive the compiler and linker, so no complicated command-line sequences need to be learnt or remembered. If you wish to learn the command-line sequences, Easy C++ will allow you to do so!

Even though this package is aimed at beginners, the C compiler conforms to the international standard for the C programming language, while the C++ compiler conforms to the early specifications for a C++ programming language. As your skills develop, you will find that Easy C++ is quite capable of handling substantial programs, and features an integrated Make facility and ARM assembler for the more adventurous.

What is C

C is a high-level programming language that was developed in the 1970s. The term high�level is used to distinguish algorithmically based languages from those that are specific to machine hardware, namely assembler languages. Basic and Pascal are both examples of high-level languages. C was derived from an older language called BCPL via an inbetween language known as B. It was developed largely by two individuals - Brian Kernighan and Dennis Ritchie.

C++ was an evolution of C, developed to implement principles for object orientated programming. This later became the replacement tool for the compilation of Unix, as it allowed the programmer to change code in stages: being an evolution of C, a lot of the original structures can still be accessed.

One of the main reasons for developing C was to use it to implement the entire UNIX operating system. This intended use for C can give us some insight into why many people view C as the only programming language worth using, and why it has survived where others have failed. Clearly, for any language to be suitable for implementing an operating system it must be flexible enough to allow operations such as accessing registers in hardware devices. It is also essential that an operating system is as bug-free as possible, as any bugs are likely to have an effect on any applications run under that operating system. To achieve this, the language used must have adequate error checking to pick up potential problems, and should ideally be 'natural' enough that code can be written easily; it is no use if the programmer continually has to allow for idiosyncrasies in the language.

Unfortunately, these criteria are to an extent mutually exclusive. Any language that is flexible enough to allow direct access to hardware is wide open to obscure bugs, as the programmer is by necessity able to perform functions which would be thought of as 'illegal'. However, C has managed to strike a happy medium and this explains partly why it is so popular.

Some may consider C/C++ inferior to assembly language programming. It is, however, best to consider the example of a standard program: 10,000 lines of C++ may become 50,000 lines of assembly code. I know which one I'd prefer to debug (it's not really difficult, is it?). The other advantage of using an implementation of C is the cross-platform potential: once you gain confidence, you'll be able to implement platform-specific libraries allowing; perhaps; for PC and Acorn versions of TypeStudio to be compiled separately!

The structure of a C system

Unlike Basic, which is an interpreted language, C is a so-called compiled language. With an interpreted language, the program is stored largely as it was entered, and when it is executed the interpreter takes each statement in turn and performs the necessary operations to implement the statement.

Interpreted languages have their advantages and disadvantages. On the plus side, they are quick and easy to use�you can simply type in a program and run it. This can be very handy for short programs, or when you simply want to try something out quickly. The main disadvantage is one of speed. Each time the program is executed the interpreter has to examine it statement by statement. Typically, the time taken to actually execute the statement makes up only a fraction of the total time taken to decode the statement and decide on what action to take. A second disadvantage is that to execute an interpreted program, a copy of the interpreter must always be present. This is not a problem with Basic on Acorn systems, as the Basic interpreter is in the RISC OS ROMs. However, on some systems, having to load the interpreter each time an application is run could be a real problem. Another, often overlooked disadvantage, is that each statement in an interpreted program is only examined when it is actually executed.

As we shall see shortly, each statement of a compiled program is looked at as a separate stage before it is run. The advantage of this is that major errors in the syntax of the program can be picked up and cured at an earlier stage. Compiled languages on the other hand operate entirely differently. The program is entered as it would be for any language, and is then saved. The program is referred to as the source code. Next, the compiler is invoked. This takes the source code and converts it piece by piece into a machine code program which performs the necessary operations. This machine code is called the object code. The major advantage of this method is that as the program is converted into machine code, it will run much faster than an interpreted equivalent. The main disadvantage is that debugging is harder because if an error is thrown up at compile time or run time you have to go back, edit the source code, and then recompile it all�a lengthy process sometimes.

Libraries and linking

In practice, the situation is more involved. C doesn't in itself contain any built-in functions to perform operations such as arithmetic or printing. Instead, these functions are provided separately in the form of a library. The functions in the library may themselves be written in C, or in assembler, or in a totally different language, and they can in turn call other library functions.

Using library functions in a C program is a two stage process. Firstly, a declaration of the function must be included in the source code. The purpose of this is to specify the entry and exit parameters of the functions. While it would be possible to type in such a declaration for each function into each program, it is more normal to include one or more standard headers in the program using a special compiler instruction. These headers are supplied with the compiler and include declarations for all the library functions.

The second stage is to combine the code for the library functions with the object code from the compiler. This is done by a tool called a linker which takes the object code and merges in the necessary routines from the library code. The output of the linker is a stand-alone program that can be executed without needing an interpreter to be loaded.

C standards

Over the years, many C compilers have been produced, and unfortunately, many variations of the language have emerged since the Kernighan and Ritchie (K&R) original. In an attempt to resolve this situation, a standard definition of C has been drawn up by ANSI (the American National Standards Institute).

One of the main areas in which the ANSI standard has helped is the range of library functions available. The original K&R definition made no mention of the library and this meant that programs couldn't be ported from one system to another, because even if a particular function existed in both libraries, you couldn't guarantee that it took the same parameters or returned the same results.

The C++ implementation offered is based upon an early specification for the language; and in some respects may not fully represent current abilities of the language. It is, however, our intention to develop this program further, ultimately offering one of the most capable compilers for RISC OS computers.

The Easy C compiler complies to the ANSI standard, but please note that it has not been validated by any standards authority. The standard ANSI library of functions is provided by the Easy C library STDCLib which calls the relevent functions in the Acorn shared C library (CLib).

How to use this guide

If you are a beginner, you should read the chapter Getting started first, which gives step-by-step instructions on how to compile, link and run a simple C program. You should then proceed to the chapter Tutorial, which is an introductory course on C programming.

If you have some C programming knowledge, read the chapter Getting started for instructions on how to compile, link and run a simple C program. With this information, you should be able to compile and link programs of your own. You may also wish to consult the chapters Using Easy C++ and The Make facility which give a full description of the options available in Easy C. For details of the functions provides, consult the chapters ANSI functions and RISC OS functions.

This user guide assumes that you are familiar with the RISC OS desktop and filing systems. If you are not, please read the relevant sections of your computer user guides.

On-line help can be obtained using the RISC OS interactive help system with which Easy C is compatible.

APDL and ProAction

 Index